package proto

import (
	"fmt"
	"google.golang.org/protobuf/proto"
	"time"
)

func handle() {
	s1 := &Student{} //第一个学生信息
	s1.Name = "jz01"
	s1.Age = 23
	s1.Address = "cq"
	s1.Cn = ClassName_class2 //枚举类型赋值
	ss := &Students{}
	ss.Person = append(ss.Person, s1) //将第一个学生信息添加到Students对应的切片中
	s2 := &Student{}                  //第二个学生信息
	s2.Name = "jz02"
	s2.Age = 25
	s2.Address = "cd"
	s2.Cn = ClassName_class3
	ss.Person = append(ss.Person, s2) //将第二个学生信息添加到Students对应的切片中
	ss.School = "cqu"
	fmt.Println("Students信息为：", ss)

	// Marshal takes a protocol buffer message
	// and encodes it into the wire format, returning the data.
	now := time.Now()
	buffer, _ := proto.Marshal(ss)
	fmt.Println("序列化之后的信息为：", buffer)
	// 	Use UnmarshalMerge to preserve and append to existing data.
	data := &Students{}
	proto.Unmarshal(buffer, data)
	fmt.Println("反序列化之后的信息为：", data)
	fmt.Printf("cost time: %vns\n", time.Since(now).Nanoseconds())
}

func marshal() {
	// target
	buffer := []byte{10, 14, 10, 4, 106, 122, 48, 49, 16, 23, 26, 2, 99, 113, 32, 1, 10, 14, 10, 4, 106, 122, 48, 50, 16, 25, 26, 2, 99, 100, 32, 2, 18, 3, 99, 113, 117}
	data := &Students{}
	//unmarshal person:{name:"jz01" age:23 address:"cq" cn:class2} person:{name:"jz02" age:25 address:"cd" cn:class3} school:"cqu"
	err := proto.Unmarshal(buffer, data)
	if err != nil {
		_ = fmt.Errorf("err:%v", err.Error())
		return
	}
}
